<?php
declare(strict_types=1);

namespace Modules\Demo\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Demo\Models\Demo;
use Modules\Demo\Http\Requests\DemoRequest;
use Illuminate\Http\Request;


class DemoController extends Controller
{
    public function __construct(
        protected readonly Demo $model
    ){}

    /**
     * @return mixed
     */
    public function index(): mixed
    {
        return $this->model->setBeforeGetList(function ($query) {
            return $query->with('casClass');
        })->getList();
    }

    /**
     * @param DemoRequest $request
     * @return mixed
     */
    public function store(DemoRequest $request)
    {
        return $this->model->storeBy($request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        $data = $this->model->firstBy($id);
//        $data->one_file = $data->one_file ? json_decode($data->one_file, true) : null;
//        $data->many_files = $data->many_files ? json_decode($data->many_files, true) : null;
        return $data;
    }

    /**
     * @param DemoRequest $request
     * @param $id
     * @return mixed
     */
    public function update($id, DemoRequest $request)
    {
        return $this->model->updateBy($id, $request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }

    /**
     * 状态开关
     * @param $id
     * @return bool
     */
    public function enable($id)
    {
        return $this->model->toggleBy($id);
    }
}
